﻿$k.fn.pin = function (config) {
    var obj = function (selector, config) {
        this.selector = selector;
        this.config = $.extendEx({}, this.defConfig, config);
    }
    obj.prototype.defConfig = {
        left: 'right',
        top: 'bottom',
        leftAdd: 0,
        topAdd: 0,
        bottomAdd: 0,
        rightAdd: 0,
        centerAdd:0,
        middleAdd:0
    };
    obj.prototype.pin = function () {
        var _obj = this;
        var target = $(_obj.config.target);
        var left;
        var top;
        var width = _obj.selector.width();
        var height = _obj.selector.height();
        //left
        if (_obj.config.left == 'left') {
            left = target.offset().left;
        }
        else if (_obj.config.left == 'center') {
            left = target.offset().left + target.width() / 2;
        }
        else if (_obj.config.left == 'right') {
            left = target.offset().left + target.width();
        }
        left += _obj.config.leftAdd;

        //right
        if (!_obj.config.left) {
            if (_obj.config.right == 'left') {
                left = target.offset().left - width;
            }
            else if (_obj.config.right == 'center') {
                left = target.offset().left + target.width() / 2 - width;
            }
            else if (_obj.config.right == 'right') {
                left = target.offset().left + target.width() - width;
            }
            left += _obj.config.rightAdd;
        }
        else {
            if (_obj.config.right == 'left') {
                width = target.offset().left - left;
            }
            else if (_obj.config.right == 'center') {
                width = target.offset().left + target.width() / 2 - left;
            }
            else if (_obj.config.right == 'right') {
                width = target.offset().left + target.width() - left;
            }
            width += _obj.config.rightAdd;
        }

        //top
        if (_obj.config.top == 'top') {
            top = target.offset().top;
        }
        else if (_obj.config.top == 'middle') {
            top = target.offset().top + target.height() / 2;
        }
        else if (_obj.config.top == 'bottom') {
            top = target.offset().top + target.height();
        }
        top += _obj.config.topAdd;

        //bottom
        if (!_obj.config.top) {
            if (_obj.config.bottom == 'top') {
                top = target.offset().top - target.height();
            }
            else if (_obj.config.bottom == 'middle') {
                top = target.offset().top + target.height() / 2 - height;
            }
            else if (_obj.config.bottom == 'bottom') {
                top = target.offset().top + target.height() - height;
            }
            top += _obj.config.bottomAdd;
        }
        else {
            if (_obj.config.bottom == 'top') {
                height = target.offset().top - top;
            }
            else if (_obj.config.bottom == 'middle') {
                height = target.offset().top + target.height() / 2 - top;
            }
            else if (_obj.config.bottom == 'bottom') {
                height = target.offset().top + target.height() - top;
            }
            height +=_obj.config.bottomAdd;
        }

        //center
        if (_obj.config.center == 'left') {
            left = target.offset().left - width / 2;
        }
        else if (_obj.config.center == 'center') {
            left = target.offset().left + target.width() / 2 - width / 2;
        }
        else if (_obj.config.center == 'right') {
            left = target.offset().left + target.width() - width / 2;
        }
        left += _obj.config.centerAdd;

        //middle
        if (_obj.config.middle == 'top') {
            top = target.offset().top - height / 2;
        }
        else if (_obj.config.middle == 'middle') {
            top = target.offset().top + target.height() / 2 - height / 2;
        }
        else if (_obj.config.middle == 'bottom') {
            top = target.offset().top + target.height() - height / 2;
        }
        top += _obj.config.middleAdd;

        _obj.selector.css('position', 'fixed');
        _obj.selector.css('left', left-$(document).scrollLeft());
        _obj.selector.css('width', width);
        _obj.selector.css('top', top-$(document).scrollTop());
        _obj.selector.css('height', height);

        
    }

    var _obj = new obj($(this.jo), config || {});
    $(window).resize(function () {
        _obj.pin();
    });
    $(window).scroll(function () {
        _obj.pin();
    });
    _obj.pin();
    return _obj;
}